// pages/order/order.js
var app = getApp();
var getSign = require('../../utils/getSign/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseUrl: '',
    pid:'',
    isHiddenFapiao: false,
    isHiddenbtn:false,
    Button:{},
    Create_Time:'',
    Id:0,
    Invoice_Info:'',
    Order_Number:'',
    Order_State:'',
    Pay_Time:'',
    Payable_Price:'',
    Product_Brief:'',
    Product_Number:'',
    Product_Pic:'',
    Product_Title:'',
    Purchase_Remarks:''
  },
  changeIsShow: function(){
    this.setData({
      isHiddenFapiao: !this.data.isHiddenFapiao
    })
  },
  goToShop: function(){
    wx.switchTab({
      url: '/pages/shop/shop',
    })
  },

  goToMaterial:function(e){
    if (e.currentTarget.dataset.isc) {
      if (e.currentTarget.dataset.btnid == 1) {
        //去付款
        console.log(e, '可以用')
        var memberInfo = wx.getStorageSync('memberInfo')
        var postArgs = {
          'Api_Type': '011',
          'orderId': e.currentTarget.dataset.id,
          'Member_Id': memberInfo.MemberId,
          'Member_Token': memberInfo.MemberToken
        }
        //获取
        getSign.request('https://wx-app-api.sjune.cn/yuren/API.ashx', 'POST', postArgs, '').then(
          (res) => {
            console.log(res, '支付信息')
            wx.requestPayment({
              timeStamp: res.Data.timeStamp,
              nonceStr: res.Data.nonceStr,
              package: res.Data.package,
              signType: res.Data.signType,
              paySign: res.Data.paySign,
              success: (res) => {
                console.log(res)
                wx.navigateTo({
                  url: '/pages/payed/payed?orderid=' + e.currentTarget.dataset.id,
                })
              },
              fail(res) { }
            })
          },
          (err) => {
            console.log(err.Message, '失败')
          }
        )
      } else if (e.currentTarget.dataset.btnid == 2) {
        //提交材料
        // wx.navigateTo({
        //   url: '/pages/material/material?id=' + e.currentTarget.dataset.id,
        // })
      } else if (e.currentTarget.dataset.btnid == 3) {
        //已使用
      } else if (e.currentTarget.dataset.btnid == 4) {
        //使用中
      } else if (e.currentTarget.dataset.btnid == 0) {
        //已取消
      }
    }

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      baseUrl: app.baseUrl,
      pid:options.id
    })
    this.requestPost('016', this.data.pid)
  },

  //发送请求
  requestPost: function (apitype, orderId) {
    var memberInfo = wx.getStorageSync('memberInfo')
    var postArgs = {
      'Api_Type': apitype,
      'orderId': orderId,
      'Member_Id': memberInfo.MemberId,
      'Member_Token': memberInfo.MemberToken
    }
    //获取
    getSign.request('https://wx-app-api.sjune.cn/yuren/API.ashx', 'POST', postArgs
      , '').then(
        (res) => {
          console.log(res, '订单详情')
          if (res.Status == 2) {
            wx.clearStorage({
              success: () => {
                wx.switchTab({
                  url: '/pages/index/index',
                })
              }
            })
          } else if (res.Status == 0) {
           
            var info = res.Data.Invoice_Info;
            info = JSON.parse(info);
            console.log(info)
            console.log(info.类型)
            var len = info['税号'].length;
            var xx = info['税号'].substring(4, len);
            info['税号'] = info['税号'].replace(xx, "*************")
            if (res.Data.Button == null) {
              this.setData({
                isHiddenbtn: true,
              })
            }
            this.setData({
              Button: res.Data.Button,
              Create_Time: res.Data.Create_Time,
              Id: res.Data.Id,
              Invoice_Info: info,
              Order_Number: res.Data.Order_Number,
              Order_State: res.Data.Order_State,
              Pay_Time: res.Data.Pay_Time,
              Payable_Price: res.Data.Payable_Price,
              Product_Brief: res.Data.Product_Brief,
              Product_Number: res.Data.Product_Number,
              Product_Pic: res.Data.Product_Pic,
              Product_Title: res.Data.Product_Title,
              Purchase_Remarks: res.Data.Purchase_Remarks
            })
          }else{
            wx.showLoading({
              title: 'Status' + res.Status,
            })
          }
          
        },
        (err) => {
          console.log(err.Message, '订单详情失败')
        }
      )
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})